'''
NBA球员数据网站如下：
“http://data.sports.sohu.com/nba/nba_players_rank.php?order_by=points&spm=smpc.fb-nba-home.top-dc.2.1620824904040nf6byr7”，
爬取前50名球员所有的数据打包成JSON数据或CSV数据写入到文件中
'''

import requests
from bs4 import BeautifulSoup as bs
# 获取页面信息
url = 'http://data.sports.sohu.com/nba/nba_players_rank.php?order_by=points&spm=smpc.fb-nba-home.top-dc.2.1620824904040nf6byr7'
headers = {'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'}
request = requests.get(url = url,headers = headers)
request.encoding = 'utf-8'
page_info = request.text

# 解析页面方法
content_list = []
soup = bs(page_info,'lxml')
tr_list = soup.select('.table01 > tr')
for tr in tr_list:
    content_list.append(tr.text)

file = open('player.json','w',encoding='utf-8')
file.writelines(content_list)
file.close()
print('解析完成')
